$(function(){
    ajaxFirst(1);
});

function ajaxFirst(currentPage){
    var jsonStr = {pageSize: 6, pageNum: currentPage};
    $.ajax({
        type: "get",
        url: "/blogMessage/getBlogMessage",
        data: jsonStr,
        contentType: "application/json",
        dataType: "json",
        success: function(data){
            putInArticle(data);
            putPageHelper(data, currentPage);
        }
    })
}


function putInArticle(result){
    var data = result.content;
    var volume = $("#volume"),
        listCont = $("#list-cont");
    volume.empty(),listCont.empty();
    var mes = $('<b>' + result.totalSize + '</b>');
    volume.append(mes);
    $.each(data, function (index, obj) {
        var center = $(
            '<div class="cont">' +
            '<div class="img">'+
            '<img src="../style/img/agentLogo.png" alt="">' +
            '</div>' +
            '<div class="text">' +
            '<p class="tit">'+
            '<span class="name">' + obj["blogUser"].nickname + '</span>' +
            '<span class="data">' + new Date(obj["createTime"]).toLocaleString() + '</span>' +
            '</p>' +
            '<p class="ct">' + obj.message +'</p>' +
            '</div>' +
            '</div>');
        listCont.append(center);
    });

}

function putPageHelper(data, currentPage){
    var count = data.totalSize; //总记录数
    // 总页数大于页码总数
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        // 执行一个laypage 实例
        laypage.render({
            elem: 'page-helper-guestBook',
            groups: 8,// 默认的连续出现的页码
            count: count,// 数据总数
            limit: 6,// 每页展示条数
            curr: currentPage,// 起始页
            jump: function (obj, first) {// 跳转页码时调用
                if (!first){
                    currentPage = obj.curr;
                    ajaxFirst(currentPage, 0)
                }
            }
        })
    })
}